#ifndef MODBUS_CLIENT_H
#define MODBUS_CLIENT_H

#include <stdint.h>
#include <modbus/modbus.h>
#include <stdbool.h>
#include "common.h"
#include "config.h"

typedef struct {
    modbus_t* ctx;
    config_t config;
    uint8_t link_id;    //链路id（0-3）

    volatile sig_atomic_t running;

    uint64_t total_operations;
    uint64_t successful_operations;
    uint64_t timeout_errors;
    uint64_t other_errors;

    timing_stats_t timing_stats;    //时间统计
    periodic_timer_t timer;         //周期定时器

    FILE* log_file;     // 日志文件
} modbus_client_t;

// 函数声明
int modbus_client_init(modbus_client_t* client, const config_t* config, uint8_t link_id);
void modbus_client_cleanup(modbus_client_t* client);
void* modbus_client_thread(void* arg);

int set_tcp_nodelay(modbus_t* ctx, bool enable);
int set_tcp_buffer_size(modbus_t* ctx,uint32_t send_size, uint32_t recv_size);
int set_tcp_quickack(modbus_t* ctx, bool enable);

int execute_pressure_test(modbus_client_t* client);

#endif
